﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;
using System.Web.Security;

public partial class Page_MeasurementInfoEdit : System.Web.UI.Page
{
    SystemFrameworkData SuperiorSFD;
    MeasurementInfoData mid1;
    protected void Page_Load(object sender, EventArgs e)
    {
        string strid = Request.QueryString["id"];
        string strsid = Request.QueryString["sid"];
        Guid sid = Guid.Parse(strsid);
        mid1 = null;
        if (sid == Guid.Empty)
        {
            Page.ClientScript.RegisterStartupScript(this.GetType(), "", " <script lanuage=javascript>alert('发生未知错误');closewindow(); </script>");
        }
        else
        {
            SuperiorSFD = SystemFrameworkDataDBOption.SelectByID(sid);
            TextBox1.Text = SuperiorSFD.Name;
        }
        if (strid != null && strid != "")
        {
            Guid id = Guid.Parse(strid);
            mid1 = MeasurementInfoDataDBOption.SelectByID(id);
        }
        if (!IsPostBack)
        {
            if (mid1 != null)
            {
                TextBox2.Enabled = false;
                TextBox9.Enabled = false;
                TextBox2.Text = mid1.Name;
                TextBox9.Text = mid1.CallNum;
                TextBox3.Text = mid1.DeviceType;
                TextBox5.Text = mid1.Telephone;
                TextBox6.Text = mid1.Address;
                TextBox4.Text = mid1.Adjust.ToString();
                TextBox8.Text = mid1.Remark;
                TextBox7.Text = mid1.Number1.ToString();
                TextBox10.Text = mid1.Number2.ToString();
            }
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        MeasurementInfoData mid = new MeasurementInfoData();
        mid.Name = TextBox2.Text;
        mid.CallNum = TextBox9.Text;
        mid.Telephone = TextBox5.Text;
        mid.Address = TextBox6.Text;
        mid.DeviceType = TextBox3.Text;
        mid.Adjust = int.Parse(TextBox4.Text);
        mid.Remark = TextBox8.Text;
        mid.Number1 = TextBox7.Text;
        mid.Number2 = TextBox10.Text;
        mid.SuperiorID = SuperiorSFD.ID;
        if (mid1 != null)
        {
            mid.CreateTime = mid1.CreateTime;
        }
        else
            mid.CreateTime = DateTime.Now;
        mid.UpdateTime = DateTime.Now;
        mid.Creator = Membership.GetUser().UserName;
        
        if (mid1 == null)
        {
            if (MeasurementInfoDataDBOption.SelectByCallNum(mid.CallNum) != null)
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "", " <script lanuage=javascript>alert('呼叫号重复，请检查');</script>");
                return;
            }
            MeasurementInfoDataDBOption.Insert(mid);
            Page.ClientScript.RegisterStartupScript(this.GetType(), "", " <script lanuage=javascript>alert('添加成功');GetDataAndClose(); </script>");
        }
        else
        {
            mid.ID = mid1.ID;
            MeasurementInfoDataDBOption.Update(mid);
            Page.ClientScript.RegisterStartupScript(this.GetType(), "", " <script lanuage=javascript>alert('修改成功');GetDataAndClose(); </script>");
        }
    }
}